from django.contrib.auth.models import User
from django.contrib.sites.models import Site

from bookmarks.models import Bookmark
from common.easyfeed import EasyFeed

current_site = Site.objects.get_current().name

class LatestBookmarkFeed(EasyFeed):
  def __init__(self, feed_url, paginate=True, **kwargs):
    super(LatestBookmarkFeed, self).__init__(feed_url)
    self.paginate = paginate

    if kwargs:
      self.kwargs = kwargs

  def link(self):
    return '/bookmarks/'

  def title(self):
    return '%s :: Latest Bookmarks' % current_site

  def description(self):
    return 'List of the latest 50 bookmarks posted'

  def items(self):
    return Bookmark.objects.order_by('-created_date')[:50]

  def item_link(self, item):
    return item.url

  def item_description(self, item):
    return item.description

  def item_title(self, item):
    return item.title

  def item_pubdate(self, item):
    return item.created_date

  def item_author_name(self, item):
    return item.user.username

  def item_categories(self, item):
    return item.tags


class AltFeed(EasyFeed):
  def __init__(self, feed_url, bookmark_user, paginate=False, **kwargs):
    super(AltFeed, self).__init__(feed_url)
    self.user = bookmark_user
    self.paginate = paginate

    if kwargs:
      self.kwargs = kwargs

  def link(self):
    return '/bookmarks/%s/' % self.user

  def items(self):
    if self.paginate and self.kwargs and self.kwargs.get('paginate_by'):
      return Bookmark.objects.filter(user__username__exact=self.user)[:int(self.kwargs['paginate_by'])]
    else:
      return Bookmark.objects.filter(user__username__exact=self.user)

  def title(self):
    return 'Bookmarks for %s'% self.user

  def description(self):
    return "%s's online bookmarks "% self.user

  def item_link(self, item):
    return '%s' % (item.url,)

  def item_description(self, item):
    return item.description

  def item_title(self, item):
    return '%s' % item.title

  def item_pubdate(self, item):
    return item.created_date

  def item_author_name(self, item):
    return item.user.username

  def item_categories(self, item):
    return item.tags
